package com.yf.zzmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.exception.ServiceException;
import com.yf.zzmusic.pojo.dto.album.AlbumDetailsDTO;
import com.yf.zzmusic.pojo.dto.albumRecord.AlbumRecordDeleteDTO;
import com.yf.zzmusic.pojo.entity.AlbumRecord;
import com.yf.zzmusic.mapper.AlbumRecordMapper;
import com.yf.zzmusic.pojo.vo.music.MusicTableVO;
import com.yf.zzmusic.service.IAlbumRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 专辑记录表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-18
 */
@Service
public class AlbumRecordServiceImpl extends ServiceImpl<AlbumRecordMapper, AlbumRecord> implements IAlbumRecordService {

  @Autowired
  AlbumRecordMapper albumRecordMapper;

  @Override
  public PageInfo<MusicTableVO> albumMusicList(AlbumDetailsDTO albumDetailsDTO) {
    //先取值
    Integer albumId = albumDetailsDTO.getAlbumId();
    Integer pageNum = albumDetailsDTO.getPageNum();
    Integer pageSize = albumDetailsDTO.getPageSize();
    if (getById(albumId)==null){
      throw new ServiceException(Constants.NO_CONTENT,"专辑不存在");
    }
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum=1;
      albumDetailsDTO.setPageNum(1);
    }
    if (pageSize == null) {
      pageSize = 10;
      albumDetailsDTO.setPageSize(10);
    }
    //执行自定义查询,获取音乐列表
    if (pageNum >= 1) {
      albumDetailsDTO.setPageNum((pageNum - 1) * pageSize);
    }
    //获取总数
    Integer totalCount=albumRecordMapper.countDetailsById(albumId);
    List<MusicTableVO> musicTableVOList=albumRecordMapper.albumDetailsById(albumDetailsDTO);
    PageInfo<MusicTableVO> pageInfo = new PageInfo<>();
    pageInfo.setPageSize(pageSize);
    pageInfo.setPageNum(pageNum);
    pageInfo.setTotal(totalCount);
    pageInfo.setData(musicTableVOList);
    return pageInfo;
  }

  @Override
  public boolean deleteAlbumRecord(AlbumRecordDeleteDTO albumRecordDeleteDTO) {
    QueryWrapper<AlbumRecord> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("music_id",albumRecordDeleteDTO.getMusicId());
    queryWrapper.eq("album_id",albumRecordDeleteDTO.getAlbumId());
    return remove(queryWrapper);
  }
}
